package msu.ml.tools;

import msu.ml.core.*;
import msu.ml.data.*;
import msu.ml.data.level2.*;
import msu.ml.util.*;
import org.w3c.dom.*;
import java.awt.*;
import weka.core.*;

public class SweepUploader
{
    public static void main(String [] args) throws Exception
    {
        String database = "nexrad";
        String user = "root";
        String password = "";

        String syntax = "{d$database} {u$user} {p$password} $FILE $META";

        CommandLineParser parser = new CommandLineParser(syntax);
        parser.parse(args);

        if(parser.optionHasValue("database"))
            database = parser.getOption("database").value;

        if(parser.optionHasValue("user"))
            user = parser.getOption("user").value;

        if(parser.optionHasValue("password"))
            password = parser.getOption("password").value;

        DatabaseModel db = new DatabaseModel("jdbc:mysql://localhost/" + 
            database, user, password);

        VolumeReader reader = new VolumeReader(new BlockReader(
            parser.getOption("FILE").value));
        Volume v = reader.readVolume();
        reader.close();

        Document metaDocument = XmlUtility.parseXmlFile(
            parser.getOption("META").value);

        Element metaData = (Element)(XmlUtility.xPathQuery(
            metaDocument, "//scan_meta_data").item(0));

        String volumeName = metaData.getAttribute("volume");
        String scanType = metaData.getAttribute("type");

        int classType = (scanType.equals("bio") ? 1 : 
            (scanType.equals("non") ? 0 : 2));

        PreProcessor pp = new PreProcessor();
        pp.setMinRange(20000);

        Scan rscan = pp.process(v.getSweep(0).getScan(0));
        Scan dscan = pp.process(v.getSweep(0).getScan(1));

        int volume_id = db.insertVolume(volumeName);
        int sweep_id = db.insertSweep(volume_id, classType);

        ScanDataConverter converter = new ScanDataConverter();
        converter.setConvolutionWidth(25);
        Instances data = converter.convertToInstancesComplete(volumeName, rscan, dscan);

        Polygon [] polygons = null;
        if(classType == 2)
        {
            NodeList regions = XmlUtility.xPathQuery(metaDocument, 
                "//classified_regions/classified_region");
            polygons = new Polygon[regions.getLength()];
            for(int i = 0; i < regions.getLength(); i++)
            {
                polygons[i] = new Polygon();
                NodeList points = ((Element)regions.item(i)).getElementsByTagName("point");
                for(int j = 0; j < points.getLength(); j++)
                {
                    Element point = (Element)(points.item(j));
                    try
                    {
                        polygons[i].addPoint(Integer.parseInt(point.getAttribute("x")),
                                Integer.parseInt(point.getAttribute("y")));
                    } 
                    catch(Exception e) {}
                }
            }
        }

        for(int i = 0; i < data.numInstances(); i++)
        {
            Instance inst = data.instance(i);

            if(classType != 2)
            {
                inst.setClassValue(classType); 
            }
            else
            {
                inst.setClassValue(1);
                for(int j = 0; j < polygons.length; j++)
                {
                  if(polygons[j].contains(inst.value(0), inst.value(1)))
                     inst.setClassValue(0);
                }
            }
        }

        data.deleteAttributeAt(0);
        data.deleteAttributeAt(0);


        for(int i = 0; i < data.numInstances(); i++)
        {
            db.insertPulseVolume(sweep_id, true, data.instance(i));
        }
    }
}
